#!/bin/sh
# Fairly primitive alarm clock/timer.
usage() { printf >&2 'usage: %s MINUTES\n' "${0##*/}"; exit; }
err() { printf >&2 '%s: error: %s\n' "${0##*/}" "$*"; exit 2; }
msg() { printf '%s: %s\n' "${0##*/}" "$*"; }
chkopts() {
  case $# in
    0) usage ;;
    1) ;;
    *) err "cannot have two or more arguments!" ;;
  esac
  if ! expr "$1" : '[0-9]*' >/dev/null 2>&1; then
    err "use positive integers, dappy!"
  else
    case "$1" in
      0) err "zero is not a positive integer!" ;;
      *) total=$1; totalsec=$((total * 60)) ;;
    esac
  fi
}
main() {
  chkopts "$@"
  sleep $totalsec &
  msg "${total#0} minutes left..."
  while :; do
    sleep 1m
    total=$((total - 1))
    case $total in
      5) msg "5 minutes left!" ;;
      1) msg "1 minute left!" ;;
      0) msg "time is up!"; break ;;
      *) msg "$total minutes left..." ;;
    esac
  done
}
main "$@"
